package com.zdd.ehe.config;

import com.zdd.ehe.Realm.AccountRealm;
import com.zdd.ehe.filter.AccountFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @title:ShiroConfig
 * @author:ZhuiZhu
 * @description:shiro配置类
 * @createDate: 2023/3/14  14:00
 */
@Configuration
public class ShiroConfig {


    @Bean("securityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("AccountRealm")AccountRealm accountRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(accountRealm);
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager")DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        //AccountFilter
        Map<String, Filter> filters = new HashMap<>();
        filters.put("AccountRealm", new AccountFilter());
        shiroFilter.setFilters(filters);
        Map<String, String> filterMap = new LinkedHashMap<>();

        //这里放置自己通行的静态资源
//        filterMap.put("/", "anon");
//        filterMap.put("/downloadFile","anon");
//        filterMap.put("/parseByName","anon");
//
//        //静态资源不拦截
//        filterMap.put("/202108*/**", "anon");
//        filterMap.put("/config/**", "anon");
//        是不是必须加上/代表是static 目录下面的
//        filterMap.put("/index.html", "anon");

        //================================//

        // 图像识别模块
        filterMap.put("/predict/image", "anon");

        // 大屏显示模块
        filterMap.put("/show/news", "anon");
        filterMap.put("/show/pest", "anon");
        filterMap.put("/show/provincePest", "anon");
        filterMap.put("/show/provinceProduce", "anon");

        // 智能问答模块
        filterMap.put("/question", "anon");

        // 知识图片模块
        filterMap.put("/knowledgeGraph/queryDisease", "anon");
        filterMap.put("/knowledgeGraph/show", "anon");

        // 登录模块
        filterMap.put("/account/login", "anon");
        filterMap.put("/account/logout", "anon");

        // 测试模块
        filterMap.put("/test/test1","authc");
        filterMap.put("/test/test2","anon");

        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    @Bean("AccountRealm")
    public AccountRealm getMyRealm(){
        return new AccountRealm();
    }

    @Bean
    public AccountFilter getAccountFilter(){
        return new AccountFilter();
    }

}
